package xzy.com.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import xzy.com.config.jwt.annotation.PassToken;
import xzy.com.entity.domain.Users;
import xzy.com.entity.vo.ResponseBean;
import xzy.com.service.UserService;


@RestController
@RequestMapping("/api/user")
public class UserController {
	@Autowired
	private UserService userService;

	// 此注解，对前端操作请求，使用登陆用户的token验证
	@PassToken
	@PostMapping("/save")//添加用户
	public ResponseBean add(@RequestBody Users user) {
		System.out.println(user);
		user.setCreateTime(new Date());
		ResponseBean responseBean = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "添加数据失败", null);
		if (user != null) {
			Boolean result = userService.save(user);
			if (result) {
				responseBean.setCode(HttpStatus.OK.value());
				responseBean.setMsg("成功添加1条数据");
				responseBean.setData(1);
				return responseBean;
			}
			return responseBean;
		}
		return responseBean;
	}

	@PassToken
	@DeleteMapping("/delete/{id}")//根据id删除用户
	public ResponseBean delete(@PathVariable("id") Long id) {
		boolean isDel = userService.removeById(id);
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除数据失败!", isDel);
		if (isDel) {
			rb.setCode(HttpStatus.OK.value());
			rb.setMsg("成功删除数据！");
		} else {
			return rb;
		}
		return rb;
	}

	// 分页处理
	// 此注解，对前端操作请求，使用登陆用户的token验证
	@PassToken
	//@RequestMapping(value = "/page", method = RequestMethod.GET)
	@GetMapping("/page")
	public ResponseBean listPages(@RequestParam("pages") int pages, @RequestParam("size") int size) {
		if (pages < 0 || size < 0) {
			return null;
		}
		Map<String, Object> map = userService.listPages(pages, size);
		System.out.println("分页："+map);
		ResponseBean rb = new ResponseBean(HttpStatus.OK.value(), "分页请求成功", map);
		return rb;
	}

	// 搜索查询分页处理
	@PassToken
	//@RequestMapping(value = "/search", method = RequestMethod.GET)
	@GetMapping("/search")
	public ResponseBean  searchListPages(@RequestParam("name") String name,
			@RequestParam("address") String address, @RequestParam("pages") int pages, @RequestParam("size") int size) {
		if (pages < 0 || size < 0) {
			return null;
		}
		Map<String, Object> map=userService.searchListPages(name, address, pages, size);
		ResponseBean rb = new ResponseBean(HttpStatus.OK.value(), "分页搜索成功", map);
		return rb;
	}
	@PassToken
	@PutMapping("/update")//更新用户
	public ResponseBean updateUser(@RequestBody Users user) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "更新失败", null);
		if (user != null) {// 核验数据
			boolean result = userService.updateById(user);
			System.out.println(user + "更新数据：" + result);
			rb.setCode(HttpStatus.OK.value());
			rb.setMsg("更新成功！");
			rb.setData(result);
			return rb;
		}
		return rb;
	}
	@PassToken
	@DeleteMapping("/delete")//删除用户
	public ResponseBean delete(@RequestParam("id") int id) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除失败", null);
		if (id > 0) {
			boolean result = userService.removeById(id);
			rb.setCode(HttpStatus.OK.value());
			rb.setMsg("删除成功！");
			rb.setData(result);
			return rb;
		}
		return rb;
	}

	//传入的json数组key=list，为一组整数索引
	@PassToken
	@DeleteMapping("/deleteAll")
	public ResponseBean deleteAll(@RequestBody JSONObject jsonObject) {
		ResponseBean rb = new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "批量删除失败", null);
		JSONArray jsonArray = jsonObject.getJSONArray("list");
		List<Integer> idList = jsonArray.toJavaList(Integer.class);
		// idList.forEach(System.out::println);
		// 批量删除
		boolean result = false;
		result = userService.removeByIds(idList);
		if (result) {
		rb.setCode(HttpStatus.OK.value());
		rb.setMsg("批量删除成功！");
		rb.setData(result);
		return rb;	
		}		
		return rb;
	}
}
